import uitest
import uicls
import uiconst
import trinity
import uiutil

class NewFrame(uicls.NewContainer):
    __guid__ = 'uicls.NewFrame'
    default_name = 'framesprite'
    default_left = 0
    default_top = 0
    default_width = 0
    default_height = 0
    default_color = (1.0, 1.0, 1.0, 1.0)
    default_align = uiconst.TOALL
    default_frameConst = uiconst.FRAME_BORDER1_CORNER0
    default_state = uiconst.UI_DISABLED

    def ApplyAttributes(self, attributes):
        self._frame = trinity.Tr2Sprite2dFrame()
        self._frame.blendMode = trinity.TR2_SBM_BLEND
        self.children.append(self._frame)
        self._offset = 0
        uicls.NewContainer.ApplyAttributes(self, attributes)
        color = attributes.get('color', self.default_color)
        if (color is not None):
            self.color = color
        self.LoadFrame(attributes.get('frameConst', self.default_frameConst))



    def OnSetAttr(self, k, v):
        uicls.NewContainer.OnSetAttr(self, k, v)
        if (k == 'color'):
            self.SetColor(v)



    def SetAlpha(self, alpha):
        current = list(self._frame.color)
        current[3] = alpha
        self._frame.color = current



    def GetAlpha(self):
        return self._frame.color[-1]



    def SetCornerSize(self, cornerSize):
        self._frame.cornerSize = cornerSize



    def GetCornerSize(self):
        return self._frame.cornerSize



    def SetColor(self, color):
        self._frame.color = color


    SetRGB = SetColor

    def SetOffset(self, offset):
        self._offset = offset
        self.UpdateAlignment()



    def GetOffset(self):
        return self._offset



    def UpdateAlignment(self, budget = None):
        uicls.NewContainer.UpdateAlignment(self)
        self._frame.displayX = self._offset
        self._frame.displayY = self._offset
        self._frame.displayWidth = (self.displayWidth + (-self._offset * 2))
        self._frame.displayHeight = (self.displayHeight + (-self._offset * 2))



    def LoadFrame(self, frameConst = None):
        (iconNo, cornerSize, offset,) = (frameConst or uiconst.FRAME_BORDER1_CORNER0)
        if ('uicore_' in iconNo):
            resPath = (iconNo.replace('uicore_', 'res:/uicore/texture/icons/') + '.dds')
        elif ('ui_' in iconNo):
            resPath = (iconNo.replace('ui_', 'res:/ui/texture/icons/') + '.dds')
        else:
            resPath = iconNo
        self._frame.resPath = resPath
        self.SetCornerSize(cornerSize)
        self.SetOffset(offset)




